# this is a dataclass module dealing with weather station data

class weatherDB:
    
    # constants
    dateCol = 2
    windDirCol = 3 # wind direction
    windSpeCol = 4 # wind speed
    CLG_Col = 6  # CLOUD CEILING--LOWEST OPAQUE LAYER
    temCol = 21 # temperature F
    dewCol = 22 # dew point F
    stpCol = 25 # station pressure
    weather = {} # a hashmap data structure
    cateList = ['min','windDir','windSpe','CLG','temp','dew','pres']
    catDict = {}
    index = 0
    for cat in cateList:
        catDict[cat] = index
        index += 1
    
    def __init__(self,fileName, startHour = 6, endHour = 17):
 
        self.weather = {} # clear the content
        
        preKey = 'dummy'
        self.weather[preKey] = [53,220,0,722,82,22,933.4]
        import csv
        file = open(fileName,'rU')
        Reader = csv.reader(file, delimiter=',')
        lineNum = 0
        for row in Reader:      
            if lineNum > 0:
                date = row[self.dateCol]
                mon = date[4:6]
                day = date[6:8]
                hr = date[8:10]
                min = date[10:]
    
                if int(hr)>=startHour-1 and int(hr)<=endHour:
                    key = mon+'-'+day+'-'+hr
                    winDir = row[self.windDirCol]
                    winSpe = row[self.windSpeCol]
                    CLG = row[self.CLG_Col]
                    tem = row[self.temCol]
                    dew = row[self.dewCol]
                    pres = row[self.stpCol]
                    #print words
                    #print mon, day, hr, min, winDir, winSpe, CLG, tem, dew, pres
                    # in some cases there are data missing, ***
                    
                    #if winDir.find('*') == -1 and winSpe.find('*') == -1 and CLG.find('*') == -1 and tem.find('*') == -1 and dew.find('*') == -1 and pres.find('*') == -1\
                    #and not winDir == '' and not winSpe == '' and not CLG == '' and not tem == '' and not dew == '' and not pres == '':
                        #print 'haha'
                   
                    # in case we some data are missing
                    if not winDir.find('*') == -1:
                        winDir = self.weather[preKey][self.catDict['windDir']]    
                    if not winSpe.find('*') == -1:
                        winSpe = self.weather[preKey][self.catDict['windSpe']]
                    if not CLG.find('*') == -1:
                        CLG = self.weather[preKey][self.catDict['CLG']]
                    if not tem.find('*') == -1:
                        tem = self.weather[preKey][self.catDict['temp']]
                    if not dew.find('*') == -1:
                        dew = self.weather[preKey][self.catDict['dew']]
                    if not pres.find('*') == -1:
                        pres = self.weather[preKey][self.catDict['pres']]
                    
                    if not self.weather.has_key(key):
                        entryToAdd = [int(min),int(winDir),int(winSpe),int(CLG),int(tem),int(dew),float(pres)]
                        self.weather[key] = entryToAdd
                        prevKey = key
            lineNum += 1
 
                
                
                
                
    def getAttr(self,key,cat):
        if self.weather.has_key(key):
            return self.weather[key][self.catDict[cat]]
    def getDic(self):
        return self.weather   
        
# test code
#wDB = weatherDB('weatherData3.csv')
#print wDB.getDic() 
#print wDB.getAttr('10-08-17','pres')     
            
                    
                
                
        
    